//扑克小对象
package poker

import (
	"errors"
	"math/rand"
	"time"
)

const ERR_POKER_NO_CARD = "无牌可用"
const (
	// ♠ ♥ ♣ ♦  扑克的花色
	POKER_Card_Color_Spade   = 0
	POKER_Card_Color_Hearts  = 1
	POKER_Card_Color_Diamond = 2
	POKER_Card_Color_Club    = 3
)

type Poker struct { //扑克模块
	IsInit bool  //是否已经初始化来
	Cards  []int //原扑克
	Data   []int //洗牌后的数据

}

func (this *Poker) Init(cards ...int) { //初始化
	if !this.IsInit {
		this.Cards = cards
		this.Data = make([]int, 0, len(this.Cards))
		this.IsInit = true
	}

}
func (this *Poker) InitByRange(min, max, count int, added ...int) { //通过最大值和最小值来初始化
	if min > 0 && max > 0 && min <= max && count > 0 && !this.IsInit {
		rangeNum := (max - min + 1) * count
		addedNum := len(added)
		this.Cards = make([]int, 0, rangeNum+addedNum)
		for i := min; i <= max; i++ {
			for j := 0; j < count; j++ {
				this.Cards = append(this.Cards, i)
			}
		}
		for i := 0; i < addedNum; i++ {
			this.Cards = append(this.Cards, added[i])
		}
		this.IsInit = true
	}

}
func (this *Poker) Shuffle() { //洗牌
	this.Data = make([]int, 0, len(this.Cards))
	r := rand.New(rand.NewSource(time.Now().Unix()))
	for _, i := range r.Perm(len(this.Cards)) {
		this.Data = append(this.Data, this.Cards[i])
	}
}
func (this *Poker) GetOverNum() int { //获取剩余数量
	return len(this.Data)
}
func (this *Poker) Deal() (overNum int, card int, err error) { //发牌
	overNum = len(this.Data)
	if overNum < 1 {
		err = errors.New(ERR_POKER_NO_CARD)
	}
	card = this.Data[0]
	this.Data = this.Data[1:]
	overNum--
	return
}
func (this *Poker) DealByNum(num int) (overNum int, card []int, err error) { //批量发牌
	overNum = len(this.Data)
	if overNum < num && num > 0 {
		err = errors.New(ERR_POKER_NO_CARD)
	}
	card = this.Data[:num]
	this.Data = this.Data[num:]
	overNum -= num
	return
}
